package com.flyme.module.exam.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.utils.JsonUtils;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.exam.client.entity.ExaDuan;
import com.flyme.module.exam.client.entity.Exam;
import com.flyme.module.exam.client.entity.ExamOpt;
import com.flyme.module.exam.client.entity.ExamType;
import com.flyme.module.exam.client.enums.AbilityItemEnum;
import com.flyme.module.exam.client.enums.LevelTypeEnum;
import com.flyme.module.exam.client.vo.ExamItem;
import com.flyme.module.exam.provider.mapper.ExamMapper;
import com.flyme.module.exam.provider.service.ExamOptService;
import com.flyme.module.exam.provider.service.ExamService;
import com.flyme.module.exam.provider.service.ExamTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 题库管理接口实现
 */
@Service
public class ExamServiceImpl extends BaseServiceImpl<ExamMapper, Exam> implements ExamService {

    @Autowired
    private ExamOptService examoptService;
    @Autowired
    private ExamTypeService examtypeService;

    private static Map<String, String> map = new HashMap<>();

    static {
        map.put("item1", "A");
        map.put("item2", "B");
        map.put("item3", "C");
        map.put("item4", "D");
        map.put("item5", "E");
        map.put("item6", "F");
        map.put("item7", "G");
        map.put("item8", "H");
        map.put("item9", "I");
        map.put("item10", "J");
    }

    @Autowired
    private ExamOptService examOptService;

    @Override
    public Boolean add(Exam exam) {
        save(exam);
        List<ExamOpt> list = getExamItemList(exam);
        return examOptService.saveBatch(list);
    }

    @Override
    public Boolean update(Exam exam) {
        examOptService.deleteByExamId(exam.getId());
        List<ExamOpt> list = getExamItemList(exam);
        examOptService.saveBatch(list);
        return saveOrUpdate(exam);
    }

    @Override
    public ResultBody delByIds(String[] ids) {
        remove(q().in("id", ids));
        return ResultBody.success("刪除成功");
    }

    @Override
    public Boolean set(Long id, String column, Object val) {
        return update(new UpdateWrapper<Exam>().set(column, val).eq("id", id));
    }

    private List<ExamOpt> getExamItemList(Exam exam) {
        List<ExamItem> examItems = JsonUtils.toList(exam.getExamItems(), ExamItem.class);
        List<ExamOpt> list = new ArrayList<>();
        int i = 1;
        for (ExamItem examItem : examItems) {
            ExamOpt examOpt = new ExamOpt();
            String examOptionCode = map.get("item" + i++);
            examOpt.setExamOptionName(examItem.getValue());
            examOpt.setExamOptionCode(examOptionCode);
            examOpt.setExamId(exam.getId());
            examOpt.setCorrect(examItem.getCorrect());
            list.add(examOpt);
        }
        return list;
    }

    /**
     * 根据评测类型查询题目
     */
    public List<Map<String, Object>> selectByItem(ExamType abilityitem, String duanLevel, String itemCode, Integer num){
        List<Map<String, Object>> list = new ArrayList<>();
        Integer easyNum = 2;
        Integer mediumNum = 2;
        Integer difficultyNum = 1;
        String itemId = String.valueOf(abilityitem.getId());
        List<Map<String, Object>> easyList = selectExam( itemId, LevelTypeEnum.easy.code, easyNum, duanLevel);
        List<Map<String, Object>> mediumList = selectExam( itemId, LevelTypeEnum.medium.code, mediumNum, duanLevel);
        List<Map<String, Object>> difficultyList = selectExam( itemId, LevelTypeEnum.difficulty.code, difficultyNum, duanLevel);
        list.addAll(easyList);
        list.addAll(mediumList);
        list.addAll(difficultyList);
        int i = num;
        for(Map<String, Object> exam : list){
            List<Map<String, Object>> opts = examoptService.selectByExam(exam.get("id").toString());
            exam.put("opts", opts);
            exam.put("num", ++i);
            if(ObjectUtils.isNotEmpty(itemCode) && itemCode.equals(AbilityItemEnum.synthesis.code)){
                exam.put("targetEntity", AbilityItemEnum.synthesis.text);
            }else{
                exam.put("targetEntity", examtypeService.getById(exam.get("examTypeId").toString()).getExamTypeName());
            }
        }
        return list;
    }

    /**
     * 查询题目
     */
    public List<Map<String, Object>> selectExam(String abilityitemId, String level, Integer num, String duanLevel){
        return listMaps(q().select("id","examTitle","examTypeId","targeType examType","answeringTime","targetEntity").eq("examTypeId", abilityitemId).
                eq("level", level).eq("duanLevel", duanLevel).orderByDesc("RAND()").last("LIMIT "+num));
    }

    /**
     * 根据评测类型查询题目
     */
    @Override
    public List<Map<String, Object>> selectByItem2(ExamType abilityitem, ExaDuan duan, String itemCode, Integer num){
        List<Map<String, Object>> list = new ArrayList<>();
        String itemId = String.valueOf(abilityitem.getId());
        String levelType = "";
        switch (duan.getDuanStars()) {
            case 1 :
                levelType = LevelTypeEnum.easy.code;
                break;
            case 2 :
                levelType = LevelTypeEnum.medium.code;
                break;
            case 3 :
                if(duan.getDuanLevel().equals("bronze")){
                    levelType = LevelTypeEnum.difficulty.code;
                    break;
                }
                levelType = LevelTypeEnum.medium.code;
                break;
            case 4 :
                if(duan.getDuanLevel().equals("silver") || duan.getDuanLevel().equals("gold")){
                    levelType = LevelTypeEnum.difficulty.code;
                    break;
                }
                levelType = LevelTypeEnum.medium.code;
                break;
            case 5 :
                levelType = LevelTypeEnum.difficulty.code;
                break;
            default:
                break;
        }
        List<Map<String, Object>> subjectList = selectExam( itemId, levelType, 5, duan.getDuanLevel());
        list.addAll(subjectList);
        int i = num;
        for(Map<String, Object> exam : list){
            List<Map<String, Object>> opts = examoptService.selectByExam(exam.get("id").toString());
            exam.put("opts", opts);
            exam.put("num", ++i);
            if(ObjectUtils.isNotEmpty(itemCode) && itemCode.equals(AbilityItemEnum.synthesis.code)){
                exam.put("targetEntity", AbilityItemEnum.synthesis.text);
            }else{
                exam.put("targetEntity", examtypeService.getById(exam.get("examTypeId").toString()).getExamTypeName());
            }
        }
        return list;
    }
}
